package org.tsp.android.GUI_Menus;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.ImageView;

public class Activity_2 extends Activity {

	private SlideShowView ssView = null;
	
	private Handler mHandler = new Handler();
	
	private int image_index = 0;
	
	private Boolean slideshow_started = false;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ssView = new SlideShowView(this);
        setContentView(ssView);
        startSlidesow();//TODO
    }
	
	@Override
	public void onResume()
	{
		super.onResume();

		if ( slideshow_started )
			mHandler.postDelayed(mUpdateTimeTask, 0);
	}
	
	@Override 
	public void onPause()
	{
		super.onPause();
		
		mHandler.removeCallbacks(mUpdateTimeTask);
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		
	}
	
	public void startSlidesow()
    {
    	slideshow_started = true;
    	mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, 0);
    }
	
	private Runnable mUpdateTimeTask = new Runnable() {
	   public void run() {
		   String[] files;
		   mHandler.postDelayed(mUpdateTimeTask, 3000);
		   try {
			AssetManager assetManager = getAssets();
			files = assetManager.list("Images");
			if ( files.length == 0 )
				return;
			loadImageAssets("Images" + File.separator + files[image_index++]);
			if ( image_index == files.length )
				image_index = 0;
		   } catch (IOException e) {
			   e.printStackTrace();
		   }	
	   }
	};
	 	
 	private void loadImageAssets(String image)
    {
    	AssetManager assetManager = getAssets();
    	InputStream inputStream = null;
    	
    	try {
			inputStream = assetManager.open(image);
			loadImage(inputStream);
			inputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }
    
 /*   private void loadImage(InputStream img_stream)
    {
    	final Bitmap aBitmap = BitmapFactory.decodeStream(img_stream);
		loadBmp(aBitmap);
    }   
*/    private void loadImage(InputStream img_stream)
    {
    	final Bitmap aBitmap = BitmapFactory.decodeStream(img_stream);
		ssView.setBitmap(aBitmap);
    }

}
